<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">欢迎登录E打电数据系统</h2>
      <form @submit.prevent="handleSubmit" class="form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            v-model="username"
            id="username"
            type="text"
            placeholder="请输入用户名"
            required
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            v-model="password"
            id="password"
            type="password"
            placeholder="请输入密码"
            required
          />
        </div>
        <div class="form-group" style="display: flex; align-items: center;">
          <input type="checkbox" v-model="remember" id="remember" />
          <label for="remember" style="margin-left: 8px;">记住密码</label>
        </div>
        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<script>
import router from '@/router'
import { login } from '@/api/index' 
import md5 from 'js-md5'
import iconv from 'iconv-lite'

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: '',
      remember: true,
      loading: false
    }
  },
  created() {
    const savedUser = localStorage.getItem('rememberAccount')
    const savedPass = localStorage.getItem('rememberPassword')
    if (savedUser && savedPass) {
      this.username = savedUser
      // 密码不回显；可做自动登录
    }
  },
  methods: {
    async handleSubmit() {
      if (!this.username || !this.password) {
        alert('请输入用户名和密码')
        return
      }

      try {
        this.loading = true

        // 使用 GBK + MD5 加密
        const raw = 'aZx2ITszH616KSKxFVqzXSzj29nTgeF!JifbXn6Rj#RzPUr75hR!YUMM95DLZQY36BzkEwLt2!8xWhFCyw4yT8aRoTyoXCbjq5dv' +
          this.username +
          this.password
        const gbkBuffer = iconv.encode(raw, 'GBK')
        const encryptedPassword = md5(gbkBuffer)

        const res = await login({
          account: this.username,
          password: encryptedPassword
        })

        if (res.errorCode === '0000') {
            console.log('登录成功:', res.responseBody)
          const { token, userInfo } = res.responseBody

          // 保存登录信息
          localStorage.setItem('token', token)
          localStorage.setItem('userInfo', JSON.stringify(userInfo))

          // 是否记住密码
          if (this.remember) {
            localStorage.setItem('rememberAccount', this.username)
            localStorage.setItem('rememberPassword', encryptedPassword)
          } else {
            localStorage.removeItem('rememberAccount')
            localStorage.removeItem('rememberPassword')
          }

          // 跳转到主页面
          router.push({ name: 'Main' })
        } else {
          alert(res.errorMessage || '登录失败')
        }
      } catch (e) {
        console.error('登录错误：', e)
        alert('网络错误，请稍后重试')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #0F1325; /* 公司主色背景 */
}

.login-box {
  width: 400px;
  padding: 40px 30px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  animation: fadeIn 0.6s ease;
}

.title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 32px;
}

.form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

label {
  font-size: 14px;
  color: #555;
  margin-bottom: 6px;
  display: block;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.3s;
}

input[type="text"]:focus,
input[type="password"]:focus {
  border-color: #36C4FF;
  outline: none;
}

.form-group input[type="checkbox"] {
  margin: 0;
  width: 16px;
  height: 16px;
  accent-color: #36C4FF;
}

.login-btn {
  margin-top: 10px;
  width: 100%;
  padding: 12px;
  font-size: 16px;
  background: #1fbcff;
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #5ed1ff;
}

.login-btn:disabled {
  background: #a0e4ff;
  cursor: not-allowed;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
